package com.gkt.ec.ws.activity.resource;

import com.gkt.ec.ws.activity.AppConstant;
import com.gkt.ec.ws.activity.model.domain.CustomerActivityEntity;
import com.gkt.ec.ws.activity.model.domain.MarketActivityEntity;
import com.gkt.ec.ws.activity.model.pojo.BaseResponse;
import com.gkt.ec.ws.activity.service.CustomerActivityService;
import com.gkt.library.commons.shared.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Huzl
 * @version 1.0.0
 */
@Component
@Path("/activity")
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
public class CustomerActivityResource {

    @Autowired
    private CustomerActivityService customerActivityService;

    /**
     * 查询活动信息接口
     */
    @GET
    @Path("/v1.0/info/{activityId}")
    public BaseResponse getActivityInfo(@PathParam("activityId") Long activityId){
        BaseResponse baseResp ;
        MarketActivityEntity activityEntity = customerActivityService.getActivity(activityId);
        if(activityEntity != null)
            baseResp = new BaseResponse(activityEntity);
        else
            baseResp = new BaseResponse(AppConstant.EC_APP_API_ERROR_DATA_NO_FOUND,"数据不存在");
        return baseResp;
    }

    /**
     * 查询客户活动信息（活动信息和客户活动订单订单信息）--二维码页面
     */
    @GET
    @Path("/v1.0/order/{customerActivityId}")
    public BaseResponse getCustomerActivity(@PathParam("customerActivityId") Long customerActivityId){
        BaseResponse baseResp = new BaseResponse();
        CustomerActivityEntity customerActivityEntity = customerActivityService.getCustomerActivity(customerActivityId);
        if(customerActivityEntity == null){
            baseResp.setErrorCode(AppConstant.EC_APP_API_ERROR_DATA_NO_FOUND).setErrorNote("数据不存在");
        }else{
            MarketActivityEntity activityEntity = customerActivityService.getActivity(
                    customerActivityEntity.getActivityId());
            Map detail = new HashMap();
            detail.put("activity",activityEntity);
            detail.put("customerActivity",customerActivityEntity);
            baseResp.setDetail(detail);
        }
        return baseResp;
    }

    /**
     * 客户输入手机号参加活动接口
     */
    @GET
    @Path("/v1.0/attend/{customerActivityId}")
    public BaseResponse attendActivity(@QueryParam("phone") String phone,
                                       @PathParam("customerActivityId") Long customerActivityId)throws IOException{
        BaseResponse baseResp = new BaseResponse();
        CustomerActivityEntity customerActivityEntity = customerActivityService.
                getCustomerActivityByPhone(customerActivityId, phone);
        if(customerActivityEntity == null){
            baseResp.setErrorCode(AppConstant.ACTIVITY_API_ERROR_WRONG_PHONE).setErrorNote("您输入的号码不能参加活动");
            return baseResp;
        }
        MarketActivityEntity activityEntity = customerActivityService.getActivity(customerActivityEntity.getActivityId());
        Date now = new Date();
        if(StringUtils.isNoneEmpty(customerActivityEntity.getVerifyCode())){
            baseResp.setErrorCode(AppConstant.ACTIVITY_API_ERROR_REPEATED).setErrorNote("您已经报名参加了此活动");
        }else if(activityEntity.getIsLimited()==1 && activityEntity.getAttendUserCount()>=activityEntity.getLimitNumber()){
            baseResp.setErrorCode(AppConstant.ACTIVITY_API_ERROR_LIMITED_ATTEND).setErrorNote("报名人数已满");
        }else if(activityEntity.getEndTime() != null && now.after(activityEntity.getEndTime())){
            baseResp.setErrorCode(AppConstant.ACTIVITY_API_ERROR_DATE).setErrorNote("活动已结束");
        }else{
            customerActivityService.confirmAttendActivity(activityEntity,customerActivityEntity);
        }
        return baseResp;
    }

    /**
     * 商家确认使用客户活动订单接口
     */
    @GET
    @Path("/v1.0/use")
    public BaseResponse companyVerify(@QueryParam("companyId") Long companyId,@QueryParam("verifyCode") String verifyCode){
        BaseResponse baseResp = new BaseResponse();
        CustomerActivityEntity customerActivityEntity = customerActivityService.getCustomerActivityByVerifyCode(verifyCode);
        if(customerActivityEntity == null){
            baseResp.setErrorCode(AppConstant.EC_APP_API_ERROR_DATA_NO_FOUND).setErrorNote("验证码错误");
            return baseResp;
        }
        MarketActivityEntity activityEntity = customerActivityService.getActivity(customerActivityEntity.getActivityId());
        Date now = new Date();
        if(customerActivityEntity.getVerifyStatus() == AppConstant.ActivitySmsVerifyStatus.COMPANY_VERIFY){
            baseResp.setErrorCode(AppConstant.ACTIVITY_API_ERROR_REPEATED).setErrorNote("不能重复使用验证码");
        }else if(!customerActivityService.isExistActivity(customerActivityEntity.getActivityId(),companyId)){
            baseResp.setErrorCode(AppConstant.EC_APP_API_ERROR_DATA_NO_FOUND).setErrorNote("当前商家未参加此活动");
        }else if(activityEntity.getStartTime() != null && now.before(activityEntity.getStartTime())){
            baseResp.setErrorCode(AppConstant.ACTIVITY_API_ERROR_DATE).setErrorNote("活动还未开始");
        }else if(activityEntity.getEndTime() != null && now.after(activityEntity.getEndTime())){
            baseResp.setErrorCode(AppConstant.ACTIVITY_API_ERROR_DATE).setErrorNote("活动已结束");
        }else{
            customerActivityService.companyVerify(customerActivityEntity,companyId);
        }
        return baseResp;
    }

}
